package com.excilys.capicsoubank.form;

import javax.validation.constraints.NotNull;

import org.hibernate.validator.constraints.NotBlank;

import com.excilys.capicsoubank.constraints.AccountIdNotEquals;
import com.excilys.capicsoubank.constraints.AccountIsValid;
import com.excilys.capicsoubank.constraints.AmountIsValid;

/**
 * Modélise le formulaire de virement avec les contraintes sur les champs et
 * entre eux.
 * 
 * @author "Grégory Coutant"
 * 
 */
@AccountIdNotEquals
public class TransferForm {

	@NotNull
	@AccountIsValid
	private Integer accountSource;

	@NotNull
	@AccountIsValid
	private Integer accountDestination;

	@NotBlank
	@AmountIsValid
	private String amount;

	public Integer getAccountSource() {
		return accountSource;
	}

	public void setAccountSource(Integer accountSource) {
		this.accountSource = accountSource;
	}

	public Integer getAccountDestination() {
		return accountDestination;
	}

	public void setAccountDestination(Integer accountDestination) {
		this.accountDestination = accountDestination;
	}

	public String getAmount() {
		return amount;
	}

	public void setAmount(String amount) {
		this.amount = amount;
	}

	/**
	 * Retourne le montant en centimes.
	 * 
	 * @param amount
	 *            le montant
	 * @return le montant en centimes
	 */
	public static long getLongAmount(String amount) {
		String[] split = amount.split("[\\.,]");

		if (split.length == 2)
			return Long.parseLong(split[0]) * 100L + Long.parseLong(split[1]);
		else
			return Long.parseLong(split[0]) * 100L;
	}
}
